package com.cgc.venus.server.log.controller;

import com.cgc.venus.server.core.bean.response.VenusResponse;
import com.cgc.venus.server.core.bean.response.VenusResponseStatus;
import com.cgc.venus.server.core.exception.VenusException;
import com.cgc.venus.server.core.utils.ExcelUtil;
import com.cgc.venus.server.log.bean.AppLog;
import com.cgc.venus.server.log.inter.AppLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

/**
 * Created by Administrator on 2017/6/3.
 */
@Api(value = "应用日志管理",description="应用日志管理所对应服务的api")
@RequestMapping("/appLog-admin")
@RestController
public class AppLogController {
    @Autowired
    private AppLogService appLogServiceImpl;
    @ApiOperation("应用日志查询")
    @RequestMapping(value = "/appLog", method = RequestMethod.GET)
    @ResponseBody
    public VenusResponse<AppLog> appLogQuery(@RequestParam("namespaceName") String namespaceName,
                                             @RequestParam("podName") String podName,
                                             @RequestParam("timeStart") String timeStart,
                                             @RequestParam("timeEnd") String timeEnd,
                                             @RequestParam("queryKey") String queryKey,
                                             @RequestParam("currentPage") int currentPage) throws VenusException {
        AppLog appLog=appLogServiceImpl.queryAppLog(namespaceName,podName,timeStart,timeEnd,queryKey,currentPage);
        VenusResponse<AppLog> response=new VenusResponse<AppLog>(VenusResponseStatus.OK,"",appLog);
        return response;
    }

    @ApiOperation("应用日志查询1")
    @RequestMapping(value = "/appLog1", method = RequestMethod.GET)
    @ResponseBody
    public VenusResponse<AppLog> appLogQuery1(@RequestParam("namespaceName") String namespaceName,
                                             @RequestParam("podName") String podName,
                                             @RequestParam("timeStart") String timeStart,
                                             @RequestParam("timeEnd") String timeEnd,
                                             @RequestParam("queryKey") String queryKey,
                                             @RequestParam("showRecodes") int showRecodes) throws VenusException {
        AppLog appLog=appLogServiceImpl.queryAppLog1(namespaceName,podName,timeStart,timeEnd,queryKey,showRecodes);
        VenusResponse<AppLog> response=new VenusResponse<AppLog>(VenusResponseStatus.OK,"",appLog);
        return response;
    }

    @ApiOperation("应用日志查询2")
    @RequestMapping(value = "/appLog2", method = RequestMethod.GET)
    @ResponseBody
    public VenusResponse<AppLog> appLogQuery2(@RequestParam("namespaceName") String namespaceName,
                                              @RequestParam("podName") String podName,
                                              @RequestParam("timeStart") String timeStart,
                                              @RequestParam("timeEnd") String timeEnd,
                                              @RequestParam("queryKey") String queryKey,
                                              @RequestParam("showRecodes") int showRecodes) throws VenusException {
        AppLog appLog=appLogServiceImpl.queryAppLog2(namespaceName,podName,timeStart,timeEnd,queryKey,showRecodes);
        VenusResponse<AppLog> response=new VenusResponse<AppLog>(VenusResponseStatus.OK,"",appLog);
        return response;
    }

    /*
    * 日志导出功能
    * */
    @ApiOperation(value = "应用日志导出接口", notes = "应用日志导出接口")
    @RequestMapping(value = "/appLog/export", method = RequestMethod.GET)
    public void appLogExport(@RequestParam("namespaceName") String namespaceName,
                             @RequestParam("podName") String podName,
                             @RequestParam("timeStart") String timeStart,
                             @RequestParam("timeEnd") String timeEnd,
                             @RequestParam("queryKey") String queryKey,
                             @RequestParam("showRecodes") int showRecodes,
                             HttpServletResponse response) throws VenusException {
        AppLog appLog=appLogServiceImpl.queryAppLog2(namespaceName,podName,timeStart,timeEnd,queryKey,showRecodes);

        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "text/plain");
        try {
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode("log.txt", "UTF-8"));
            InputStream ins = new ByteArrayInputStream(appLog.getLogs().getBytes());
            //workbook.write(response.getOutputStream());
            OutputStream ops = response.getOutputStream();
            byte[] content = new byte[1024];
            int length = 0;
            while ((length = ins.read(content)) != -1) {
                ops.write(content, 0, length);
            }
            ops.flush();
            ops.close();
            ins.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}